<!-- https://www.jq22.com/yanshi23474 -->
<template>
  <div class="tempvue">
    <div class="progress_box">
      <div :class="isPlay ? 'progress_stop' : 'progress_play'" @click="playHandle"></div>
      <div class="progress_content" ref="progress_content" @mousemove="mousemove" @mousedown="mousedown">
        <div id="progressTime" :style="{ width: `${progressWidth}px` }"></div>
      </div>
    </div>
    <div class="slider">
      <div ref="scrollArea" id="scrollArea" @mousemove="mousemove">
        <div id="scrollHandle" ref="scrollHandle" @mousedown="mousedown"></div>
      </div>
    </div>
    <canvas id="diffusion_wave" width="600" height="600"></canvas>
  </div>
</template>

<script>
import { jqInstall, formatDate } from "./play"
import $ from "jquery"

let isDragging = false
let lastX = null
export default {
  data() {
    return {
      progressWidth: 1000,
      isPlay: false,
    }
  },
  methods: {
    playHandle() {
      this.isPlay = !this.isPlay
      if (this.isPlay) {
        progressTime.autoPlay(progressTime.playSelf)
      } else {
        progressTime.stopPlay(progressTime.playSelf)
      }
    },
    mousedown(e) {
      console.log("mouseDown", e)
      if (e.layerY < 9) {
        return
      }
      isDragging = true
      // Prevent text selection
      e.preventDefault()
    },
    mousemove(e) {
      if (isDragging && e.layerY > 20) {
        // console.log("mouseMove", e)
        let moveAmount
        if (lastX) {
          moveAmount = e.layerX - lastX
        } else {
          lastX = e.layerX
          return
        }
        lastX = e.layerX

        let scrollArea = this.$refs.progress_content
        scrollArea.scrollLeft = scrollArea.scrollLeft - moveAmount
      }
    },
    playerInit() {
      let self = this

      // let startTime = new Date(formatDate(new Date(new Date().getTime() - dayTimestamp * 5), "YYYY/MM/DD 00:00:00"))
      // let endTime = new Date(formatDate(new Date(), "YYYY/MM/DD 00:00:00"))
      // let currentTime = new Date(formatDate(new Date(), "YYYY/MM/DD 00:00:00"))

      //  new Date('2024/05/22 00:00:00')
      let startTime = new Date("2024/05/01 00:00:00")
      let endTime = new Date("2024/05/16 00:00:00")
      let currentTime = new Date("2024/05/11 00:00:00")

      // 计算两个日期之间的毫秒数差异
      const timeDifference = endTime - startTime

      // 将毫秒数转换为天数
      const dayDifference = timeDifference / (1000 * 60 * 60 * 24)

      console.log(`两个日期之间的天数是：${dayDifference}天`)

      this.progressWidth = dayDifference * 200
      let scrollArea = this.$refs.progress_content
      scrollArea.scrollLeft = ((currentTime - startTime) / (endTime - startTime)) * this.progressWidth

      $("#progressTime").ProgressTime({
        container: "progressTime",
        startTime,
        endTime,
        currentTime,
        stopTime: endTime,
        interval: 300,
        delay: 2000,
        callback: function (config) {},
        animateCallback: function (config) {
          if (self.isPlay) {
            var timer = setTimeout(function () {
              progressTime.autoPlay(progressTime.playSelf)
              clearTimeout(timer)
            }, 0)
          }
        },
      })
      self.$nextTick(() => {
        let scrollArea = self.$refs?.progress_content
        scrollArea.scrollLeft =
          ((currentTime - startTime) / (endTime - startTime)) * self.progressWidth - scrollArea.offsetWidth / 2
      })
    },
    drawWave() {
      const canvas = document.getElementById("diffusion_wave")
      const ctx = canvas.getContext("2d")
      const width = canvas.width
      const height = canvas.height
      let waves = []

      function createWave() {
        return {
          x: width / 2,
          y: height / 2,
          radius: 1,
          alpha: 1.0,
        }
      }

      function drawWave() {
        ctx.clearRect(0, 0, width, height)

        for (let i = 0; i < waves.length; i++) {
          let wave = waves[i]

          // Create radial gradient
          const gradient = ctx.createRadialGradient(wave.x, wave.y, 0, wave.x, wave.y, wave.radius)
          gradient.addColorStop(0, `rgba(255, 0, 0, 0.5)`)
          gradient.addColorStop(1, `rgba(255, 0, 0,${wave.alpha})`)

          // Draw wave
          ctx.beginPath()
          ctx.arc(wave.x, wave.y, wave.radius, 0, Math.PI * 2, false)
          ctx.closePath()
          ctx.fillStyle = gradient
          ctx.fill()

          // Update wave properties
          wave.radius += 0.2
          wave.alpha -= 0.01

          // Remove the wave if it's fully faded out
          if (wave.alpha <= 0) {
            waves.splice(i, 1)
            i--
          }
        }

        requestAnimationFrame(drawWave)
      }

      function addWave() {
        waves.push(createWave())
        setTimeout(addWave, 800) // Adjust the interval for new waves
      }

      // Start the wave animation
      drawWave()
      addWave()
    },
  },
  mounted() {
    // console.log("$", $)
    // var hourTimestamp = 3600 * 1000
    // var dayTimestamp = hourTimestamp * 24

    // console.log("date", formatDate(new Date(new Date().getTime() - dayTimestamp * 5), "YYYY/MM/DD 00:00:00"))
    jqInstall($, window, this)
    this.playerInit()
    this._mouseFn = () => {
      lastX = null
      isDragging = false
    }
    window.addEventListener("mouseup", this._mouseFn)
    this.drawWave()
  },
  destroyed() {
    window.removeEventListener("mouseup", this._mouseFn)
  },
}
</script>

<style lang="scss">
.tempvue {
  #diffusion_wave {
    border: 1px solid #000;
  }
  .slider {
    margin-top: 20px;

    .content_box {
      background: linear-gradient(to right, red, blue);
    }
    #scrollArea {
      width: 400px;
      height: 200px;
      overflow: auto;
      border: 1px solid #000;
      position: relative;
      scrollbar-width: none;
    }
    #scrollHandle {
      width: 600px;
      height: 50px;
      background-color: #333;
      background: linear-gradient(to right, red, blue);
      cursor: pointer;
    }
  }
  .progress_box {
    display: flex;
    .progress_play {
      width: 52px;
      height: 52px;
      cursor: pointer;
      background: url("~@/assets/bofang.svg") no-repeat center, #999;
      align-self: flex-end;
    }
    .progress_stop {
      width: 52px;
      height: 52px;
      cursor: pointer;
      background: url("~@/assets/zantingbofang.svg") no-repeat center, #999;
      align-self: flex-end;
    }
  }
  .progress_content {
    width: 500px;
    overflow-x: auto;
    scrollbar-width: none;
  }
  * {
    margin: 0;
    padding: 0;
    list-style: none;
    text-decoration: none;
  }
  #progressTime {
    height: 50px;
    width: 80%;
    border: 1px transparent solid;
    margin: 50px auto 0;
    display: flex;
    background-color: rgba(0, 0, 0, 0.7);
    width: 1024px;
  }
  .progressTime-left {
    width: 48px;
    height: 100%;
    display: flex;
    flex-direction: column;
    display: none;
  }
  .progressTime-left-t {
    height: 8px;
    // background-color: #25d096;
    position: relative;
  }
  .progressTime-left-b {
    flex: 1;
    position: relative;
  }
  .progressTime-left-b-start {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 0;
    height: 0;
    border: 6px solid;
    border-color: #fff #fff transparent transparent;
    -webkit-transform: rotate(45deg);
    -moz-transform: rotate(45deg);
    -o-transform: rotate(45deg);
    -ms-transform: rotate(45deg);
    transform: rotate(45deg);
    margin-left: -9px;
    margin-top: -6px;
    cursor: pointer;
  }
  .progressTime-left-b-start.stop {
    width: 20px;
    height: 12px;
    border: none;
    -webkit-transform: rotate(0deg);
    -moz-transform: rotate(0deg);
    -o-transform: rotate(0deg);
    -ms-transform: rotate(0deg);
    transform: rotate(0deg);
    cursor: pointer;
  }
  .progressTime-left-b-start.stop::before {
    content: "";
    position: absolute;
    width: 4px;
    height: 12px;
    left: 5px;
    background-color: #fff;
  }
  .progressTime-left-b-start.stop::after {
    content: "";
    position: absolute;
    width: 4px;
    height: 12px;
    left: 4px;
    background-color: #fff;
    margin-left: 10px;
  }
  .progressTime-center {
    flex: 1;
    display: flex;
    flex-direction: column;
    // overflow: auto;
  }
  .progressTime-center-t {
    height: 8px;
    background-color: rgba(255, 255, 255, 0.7);
    box-sizing: border-box;
    cursor: pointer;
    position: relative;
  }
  .progressTime-center-t-bar {
    // background-image: linear-gradient(to right, rgba(37, 208, 150, 1), rgba(45, 182, 205, 1));
    height: 100%;
    width: 0%;
  }
  .progressTime-center-t-tooltip,
  .progressTime-center-t-tooltipTemp {
    position: absolute;
    padding: 0 8px;
    height: 24px;
    line-height: 24px;
    top: -35px;
    text-align: center;
    font-size: 14px;
    color: #fff;
    background-color: gray;
    border-radius: 3px;
    -webkit-transform: translateX(-50%);
    -o-transform: translateX(-50%);
    -moz-transform: translateX(-50%);
    -ms-transform: translateX(-50%);
    transform: translateX(-50%);
    min-width: 20px;
    white-space: nowrap;
  }
  .progressTime-center-t-tooltip::after,
  .progressTime-center-t-tooltipTemp::after {
    content: "";
    position: absolute;
    top: 100%;
    left: 50%;
    margin-left: -8px;
    border: 8px solid;
    border-color: gray transparent transparent transparent;
  }
  .progressTime-center-t-tooltipTemp {
    background-color: gray;
  }
  .progressTime-center-t-tooltipTemp::after {
    border-color: #ccc transparent transparent;
  }
  .progressTime-center-t-ul {
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    z-index: 1;
  }
  .progressTime-center-t-ul::after {
    content: "";
    display: block;
    clear: both;
  }
  .progressTime-center-t-ul,
  .progressTime-center-t-ul li {
    height: 100%;
  }

  .progressTime-center-t-ul li {
    float: left;
    border-left: 1px solid #999;
    box-sizing: border-box;
    height: 110%;
  }

  .progressTime-center-c {
    height: 12px;
    line-height: 12px;
  }
  .progressTime-center-c-ul::after {
    content: "";
    display: block;
    clear: both;
  }
  .progressTime-center-c-ul,
  .progressTime-center-c-ul-li {
    height: 100%;
  }
  .progressTime-center-c-ul-li {
    float: left;
    border-left: 1px solid #999;
    border-bottom: 1px solid #999;
    box-sizing: border-box;
    position: relative;
  }
  .progressTime-center-c-ul-li span {
    position: absolute;
    color: #fff;
    font-size: 12px;
    -webkit-transform: translateX(-50%) scale(0.8);
    -moz-transform: translateX(-50%) scale(0.8);
    -ms-transform: translateX(-50%) scale(0.8);
    -o-transform: translateX(-50%) scale(0.8);
    transform: translateX(-50%) scale(0.8);
  }
  .progressTime-center-c-ul-li span::after {
    content: "";
    position: absolute;
    width: 1px;
    top: -90%;
    background-color: #fff;
    height: 9px;
    -webkit-transform: translateX(-50%);
    -moz-transform: translateX(-50%);
    -ms-transform: translateX(-50%);
    -o-transform: translateX(-50%);
    transform: translateX(-50%);
    left: 50%;
  }
  .progressTime-center-b {
    flex: 1;
  }
  .progressTime-center-b-ul::after {
    content: "";
    display: block;
    clear: both;
  }
  .progressTime-center-b-ul {
    cursor: grab;
  }
  .progressTime-center-b-ul,
  .progressTime-center-b-ul li {
    height: 100%;
  }
  .progressTime-center-b-ul li {
    display: flex;
    justify-content: center;
    align-items: center;
    float: left;
    border-left: 1px solid #999;
    /* border-bottom: 1px solid #999; */
    color: #fff;
    font-size: 12px;
    box-sizing: border-box;
  }
  .progressTime-right {
    display: none;
    height: 100%;
    width: 120px;
    position: relative;
    border-left: 1px solid #999;
  }
  .progressTime-right-now {
    height: 28px;
    line-height: 28px;
    width: 80px;
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    margin: auto;
    border-radius: 14px;
    background-color: #2db6cd;
    text-align: center;
    font-size: 12px;
    color: #fff;
    cursor: pointer;
  }
  .hide {
    display: none;
  }
}
</style>
